package com.gin.no_gay_alliance.adpter;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.response.body.nuke.MissionCheckInBody;
import com.gin.nga.response.field.LabelValue;
import com.gin.nga.response.field.mission.MissionInfo;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.databinding.ItemLabelValueBinding;
import com.gin.no_gay_alliance.databinding.ItemMissionBinding;
import com.gin.no_gay_alliance.enums.MessageWhat;
import com.gin.no_gay_alliance.viewmodel.MissionModel;

import java.util.List;

/**
 * 任务适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/13 10:07
 **/
public class MissionAdapter extends BaseBindingRecyclerViewAdapter<ItemMissionBinding, MissionModel> implements View.OnClickListener, Callback<MissionCheckInBody> {
    public MissionAdapter(Context context) {
        super(context);
    }

    public MissionAdapter(@NonNull Context context, @NonNull List<MissionModel> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemMissionBinding> holder, @NonNull ItemMissionBinding b, int position, MissionModel item) {
        //任务名称
        b.tvName.setText(item.name);
        // 签到按钮
        b.btCheckIn.setTag(item.id);
        b.btCheckIn.setOnClickListener(this);
        if (item.checkAble != null) {
            b.btCheckIn.setVisibility(View.VISIBLE);
            b.btCheckIn.setEnabled(item.checkAble);
            b.btCheckIn.setText(item.checkAble ? "签到" : "已签到");
        } else {
            b.btCheckIn.setVisibility(View.GONE);
        }
        //任务信息
        b.recyclerView.setAdapter(new BaseBindingRecyclerViewAdapter<ItemLabelValueBinding, LabelValue>(context, item.props) {
            @Override
            protected void onBindViewHolder(ViewHolder<ItemLabelValueBinding> holder, @NonNull ItemLabelValueBinding b, int position, LabelValue item) {
                b.tvLabel.setText(item.getLabel());
                b.tvValue.setText(String.valueOf(item.getValue()));
            }

            /**
             * @return 是否应用斑马纹配色
             */
            @Override
            protected boolean useZebraStripe() {
                return false;
            }
        });
    }


    @Override
    public void onClick(View v) {
        // 签到按钮
        if (v.getId() == R.id.bt_check_in) {
            int id = (int) v.getTag();
            MyApplication.app.managerFactory.getMissionManager().missionCheckIn(id, this);
        }
    }

    /**
     * 请求成功回调
     *
     * @param data 数据
     */
    @Override
    public void onSuccess(MissionCheckInBody checkInBody) {
        MissionInfo missionInfo = checkInBody.getMissionInfo();
        String message = !ObjectUtils.isEmpty(checkInBody.getMessage()) ? checkInBody.getMessage()
                : String.format("%s 签到成功", missionInfo.getName());
        MyApplication.app.handler.toast(message);
        // 抛出消息给上级
        handler.sendEmptyMessage(MessageWhat.ON_SUCCESS_CHECK_IN_REQUEST);
    }
}
